import { createApp } from 'vue'
import ViewUIPlus from 'view-ui-plus'
import App from './App.vue'
import router from './router'
import store from './store'
import 'view-ui-plus/dist/styles/viewuiplus.css'
import './assets/main.less'
import { sync } from 'vuex-router-sync'
import util from '@/libs/util'
import {currency, formatMoney} from "@/libs/currency";
import {distanceTime, format, formatHis, formatYmd} from "@/libs/format";
import rpButton from '@/components/common/rpButton.vue';

const app = createApp(App)

// 注册函数
app.config.globalProperties.$currency = currency;
app.config.globalProperties.$formatMoney = formatMoney;
app.config.globalProperties.$format = format;
app.config.globalProperties.$formatHis = formatHis;
app.config.globalProperties.$formatYmd = formatYmd;
app.config.globalProperties.$distanceTime = distanceTime;

// 创建
// app.use(ViewUI).use(router).use(currency).use(formatMoney);
app.use(router).use(ViewUIPlus).use(store).use(currency).use(formatMoney).use(format).use(formatHis).use(formatYmd).use(distanceTime).component('rp-button', rpButton);

router.beforeEach(async (to, from, next) => {
  //判断是否有标题
  const title = to.meta.title ? to.meta.title : "";
  document.title = title + " - 后台管理系统";

  // 获取token
  if (util.wsCache.get("crmToken")) {
    // 如果未拉取过用户信息，则去拉取吧
    if (!store.state.user.data.id && !to.meta.noAuth) {
      let res = store.dispatch('user/getUserInfo');
      if (res) {
        next()
      } else {
        setTimeout(() => {
          next('/login')
        }, 1500);
      }
    } else {
      next()
    }
  } else {
    // 判断是否需要登录
    if (to.meta.noAuth) {
      next()
    } else {
      next('/login')
    }
  }
});
// 合并对象
sync(store, router)

app.mount('#app')
